<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;

class UserRegister extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|max:20|unique:users,name',
            'email' => 'bail|required|email|unique:users,email',
            //'verification' => 'required',
            'password' =>array(
                'required',
                'between:8,10',
                'regex:/(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[A-Za-z0-9]/'
            ),
            'policy' => 'accepted:1'
        ];
    }

    /**
     * 错误信息提示语句
     * @return array
     */
    public function messages()
    {
        return [
            'name.required' => '请输入用户名',
            'name.max' => '用户名最大20字符',
            'name.unique' => '用户名已注册',
            'email.required' => '请输入邮箱',
            'email.email' => '邮箱格式不正确',
            'email.unique' => '邮箱已注册',
           // 'verification.required' => '请输入验证码',
            'password.required' => '请输入密码',
            'password.between' => '密码,必须介于8-10个字符',
            'password.regex' => '密码,必须包含大小写字母和数字',
            'policy.accepted' => '请勾选条款和政策'
        ];
    }

    /**
     * @param Validator $validator
     * 重写错误提示类型
     */
    protected function failedValidation(Validator $validator) {
        $error= $validator->errors()->all();
        $resultJson = app('resultJson');
        $resultJson->setMessage($this->jsonMessage)->setData($error)->errorJson();
    }
}
